<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/24/024
 * Time: 20:37
 */

namespace app\api\model;

use think\Db;
use think\facade\Session;

class CourseRecord extends \think\Model
{
    

    /**
    * 记录课程进度
    * @mid int 会员id
    * @cid int 课程id
    * @cmid int 课程目录id
    * @cm_time string 课程时间
    */
    public function _updateProgress($mid, $cid, $cmid, $cm_time){
        $courseRecord = $this->where(['mid'=>$mid, 'cid'=>$cid])->find();
        if(empty($courseRecord)){
            $this->error = '记录不存在';
            return false;
        }
        $class_id = $courseRecord['class_id'];
        //班级的总时间
        $model = new Course;
        $classAll = $model->_getCourseTimeAll($class_id);
        
        //课程总时间
        $model = new CourseMenu();
        $courseAll = $model->_getCourseTimeAll(['cid'=>$cid]);
        
        //班级观看总时间
        $model = new CourseMenuRecord;
        $classScan = $model->_getAllScanTime(['class_id'=>$class_id,'mid'=>$mid]);
        //课程观看总时间
        $courseScan = $model->_getAllScanTime(['cid'=>$cid,'mid'=>$mid]);
        
        $classProgress = 0;
        $courseProgress = 0;

        if($classAll>0){
            $classProgress = ceil($classScan/$classAll*100);
        }
        
        if($courseAll>0){
            $courseProgress = ceil($courseScan/$courseAll*100);
        }
        $nowCourseProgress = $courseRecord['progress'];
        $modelMemberClass = new MemberClass;
        $myClass = $modelMemberClass->_getInfoByWhere(['cid'=>$class_id, 'mid'=>$mid]);
        if(empty($myClass)){
            $this->error = '该课程未开通';
            return false;
        }
        
        $nowClassProgress = $myClass['progress'];
        
        if($classProgress > $nowClassProgress){
            if($classProgress>=100){
                $classProgress = 100;
            }
            $classMemberId = $myClass['id'];
            $ret = $modelMemberClass->_updateProgress($classMemberId, $classProgress);
            if(!$ret){
                $this->error = '进度更新失败';
                return false;
            }
        }
        
        if($courseProgress > $nowCourseProgress){
            if($courseProgress>=100){
                $courseProgress = 100;
            }
            $courseRecordId = $courseRecord['id'];
            $ret = $this->where(['id'=>$courseRecordId])->update(['progress'=>$courseProgress, 'updated'=>time()]);
            if(!$ret){
                $this->error = '进度更新失败';
                return false;
            }
            
        }
        
        return true;
        
    }
    

    
    /**
    * 我的观看记录
    **/
    public function _scanRecord($mid){
        $list = $this->alias('cr')
            ->field("c.id,c.title,c.image,cr.progress,cr.last_cmid, cr.scan_time, cm.title as last_title")
            ->join("course c", 'c.id = cr.cid', 'LEFT')
            ->join("course_menu cm", 'cm.id = cr.last_cmid', 'LEFT')
            ->where(['cr.mid'=>$mid])
            ->order("cr.updated desc")
            ->select()
            ->toArray();
        return $list;
    }
    
    /**
    * 通过会员id，课程id获取记录
    */
    public function _getInfoByWhere($where){
        return $this->where($where)->find();
    }
    
    /**
    * 创建会员观看记录
    */
    public function _createRecord($post){
        if(empty($post['cid'])){
            $this->error = '课程id缺失';
            return false;
        }
        if(empty($post['cmid'])){
            $this->error = '章节id缺失';
            return false;
        }
        
        
        $cid = (int)$post['cid'];
        $mid = (int)$post['mid'];
        $cmid = (int)$post['cmid'];
        $courseModel = new Course;
        $course = $courseModel->_getInfoByID($cid);
        if(empty($course)){
            $this->error = '该课程不存在';
            return false;
        }
        $class_id = $course['class_id'];
        $courseMenuModel = new CourseMenu;
        $courseMenu = $courseMenuModel->_getInfoByIdAndCid($cmid, $cid);
        if(empty($courseMenu)){
            $this->error = '该章节不存在';
            return false;
        }
        
        $this->startTrans();
        $time = time();
        $sql = "insert sy_course_record(mid, class_id, cid, last_cmid, created) select $mid, $class_id, $cid, $cmid, $time  from DUAL   where (select IFNULL(count(id), 0) from sy_course_record  where mid = $mid and cid = $cid) = 0";
        $ret = Db::execute($sql);
        if(!$ret){
            $this->error = '记录失败';
            return false;
        }
        
        $sql = "insert sy_course_menu_record(mid, class_id, cid, cmid, created) select $mid, $class_id, $cid, $cmid, $time  from DUAL   where (select IFNULL(count(id), 0) from sy_course_menu_record  where mid = $mid and cmid = $cmid) = 0";
        $ret = Db::execute($sql);
            
        if(!$ret){
            $this->error = '记录失败';
            return false;
        }
        
        
        $this->commit();
        return true;
    }
    
    
    
    /**
    * 更新会员观看记录
    */
    public function _recordMemberScan($post){
        if(empty($post['cid'])){
            $this->error = '课程id缺失';
            return false;
        }
        if(empty($post['cmid'])){
            $this->error = '章节id缺失';
            return false;
        }
        if(empty($post['scan_time'])){
            $this->error = '浏览时间缺失';
            return false;
        }
        
        $cid = (int)$post['cid'];
        $mid = (int)$post['mid'];
        //$scan_time = strtotime($post['scan_time']);
        //$scan_time = $this->transform_time($post['scan_time']);
        $scan_time = $post['scan_time'];
        $cmid = (int)$post['cmid'];
        $courseModel = new Course;
        $course = $courseModel->_getInfoByID($cid);
        if(empty($course)){
            $this->error = '该课程不存在';
            return false;
        }
        $class_id = $course['class_id'];
        $courseMenuModel = new CourseMenu;
        $courseMenu = $courseMenuModel->_getInfoByIdAndCid($cmid, $cid);
        if(empty($courseMenu)){
            $this->error = '该章节不存在';
            return false;
        }
        
        $this->startTrans();
        
        //更新章节记录
        $ret = $this->where([['mid','=', $mid], ['cid', '=', $cid], ['class_id', '=', $class_id]])->update(['last_cmid'=>$cmid, 'scan_time'=>$scan_time, 'updated'=>time()]);
        if(!$ret){
            $this->error = '记录失败';
            return false;
        }
        $model = new CourseMenuRecord;
        $ret = $model->_recordMemberScan($post);
        
        if(!$ret){
            $this->error = $model->getError();
            return false;
        }
        
        //更新课程进度与章节进度
        $ret = $this->_updateProgress($mid, $cid, $cmid, $scan_time);
        if(!$ret){
            $this->error = $this->getError();
            return false;
        }
        $this->commit();
        return true;
    }

    public function transform_time($time){
        $time_arr = explode(':',$time);
        if(count($time_arr)==3){
            $hour_sys   = $time_arr[0]*60*60;
            $munute_sys = $time_arr[1]*60;
            $sys_sys    = $time_arr[2];
            $back_sys = $hour_sys+$munute_sys+$sys_sys;
        }elseif (count($time_arr)==2){
            $munute_sys = $time_arr[0]*60;
            $sys_sys    = $time_arr[1];
            $back_sys = $munute_sys+$sys_sys;
        }else{
            $back_sys = 0;
        }
        return $back_sys;
    }
}